
__PACKAGE__->add_step(

    comment => [],

    pre_run  => [],
    do       => [],
    post_run => [],

    rollback => [],
);

########################

__PACKAGE__->add_step(
    comment => "check that all postcodes are filled in",
    test    => sub {
        my $self = shift;
        my ($missing_count) = $self->dbh->selectrow_array(
            "select count(*) from listings where postcode is NULL");
        return $missing_count == 0;
    },

);

# FIXME - check SQL syntax
__PACKAGE__->add_step(
    comment  => "add lat long columns to table",
    do       => "alter table listings add column lat float, long float",
    rollback => "alter table listings remove columns lat, long",
);

__PACKAGE__->add_step(
    comment => "convert all postcodes to lat long",
    do      => sub {
        my $self = shift;
        my $dbh  = $self->dbh;

        my $query  = $dbh->prepare("select distinct postcode from listings");
        my $update = $dbh->prepare(
            "update listings set lat = ?, long = ? where postcode = ?");

        while ( my ($postcode) = $query->fetchrow_array ) {
            my ( $lat, $long ) = $self->pc_to_lat_long($postcode);
            $update->execute( $lat, $long, $postcode );
        }

        return 1;
    },
    rollback => "update listings set lat = null, long = null",
);

__PACKAGE__->add_step(
    comment => "check that the lat longs are all filled in",
    test    => sub {
        my $self = shift;
        my ($missing_count) = $self->dbh->selectrow_array(
            "select count(*) from listings where lat is NULL or long is null");
        return $missing_count == 0;
    },
);

__PACKAGE__->add_step(
    comment => "set lat long columns to be not null",
    do      => "alter table listings set lat not null, long not null",
    rollback => "",    # none needed
);

